using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_usercp_pm_privatemessages : Controls.DataManagerUserControl
{
    public enum Mailbox
    {
        Inbox,
        Outbox,
        Sent
    }

    public Mailbox MsgMode
    {
        get
        {
            if (ViewState["MsgMode"] != null)
            {
                return (Mailbox)ViewState["MsgMode"];
            }
            else
            {
                return Mailbox.Inbox;
            }
        }
        set
        {
            ViewState["MsgMode"] = value;
            CurrentPage = 1;
        }
    }

    private int CurrentPage
    {
        get
        {
            if (ViewState["Page"] != null)
            {
                return (int)ViewState["Page"];
            }
            else
            {
                return 1;
            }
        }
        set
        {
            ViewState["Page"] = value;
        }
    }

    protected override void ItemChanged()
    {
        Refresh();
    }

    protected void ShowMessage(object sender, CommandEventArgs e)
    {
        ShowMessage(DAL.PrivateMessages.GetByIDAndUser(Convert.ToInt32(e.CommandArgument), CurrentUser));
    }

    public void ShowMessage(DAL.PrivateMessage pm)
    {
        mviewPMs.SetActiveView(viewMessage);
        ViewPM1.CurrentMessage = pm;
    }

    public void ComposeNewMessage(DAL.User recipient)
    {
        mviewPMs.SetActiveView(viewNew);
        Newmessage1.CurrentUser = CurrentUser;
        Newmessage1.SetRecipient(recipient);
    }

    public void ComposeNewMessage()
    {
        ComposeNewMessage(null);
    }

    public void Refresh()
    {
        if (CurrentUser != null)
        {
            mviewPMs.SetActiveView(viewMsgs);
            Newmessage1.CurrentUser = CurrentUser;

            DAL.DataManagerPage<DAL.PrivateMessage> pmPage = null;
            switch (MsgMode)
            {
                case Mailbox.Outbox:
                    lblMailbox.Text = "Outbox";
                    pmPage = DAL.PrivateMessages.GetOutboxByUserAndPage(CurrentUser, CurrentPage);
                    Page.Title = "FSTDT Forums: " + "Private Messages: Outbox";
                    break;
                case Mailbox.Sent:
                    lblMailbox.Text = "Sent Messages";
                    pmPage = DAL.PrivateMessages.GetSentboxByUserAndPage(CurrentUser, CurrentPage);
                    Page.Title = "FSTDT Forums: " + "Private Messages: Sent Message";
                    break;
                default:
                    lblMailbox.Text = "Inbox";
                    pmPage = DAL.PrivateMessages.GetInboxByUserAndPage(CurrentUser, CurrentPage);
                    Page.Title = "FSTDT Forums: " + "Private Messages: Inbox";
                    break;
            }
            rptPages.DataSource = Helpers.PageNumberHelper.GetPages(pmPage.Pages, CurrentPage, 10);
            rptPages.DataBind();
            gridMsgs.DataSource = pmPage.Items;
            gridMsgs.DataBind();
        }
        else
        {
            mviewPMs.SetActiveView(viewError);
        }
    }

    protected void SwitchPage(object sender, CommandEventArgs e)
    {
        CurrentPage = Convert.ToInt32(e.CommandArgument);
        Refresh();
    }

    protected void MessageCreated(object sender, DAL.PrivateMessage message)
    {
        ShowMessage(message);
    }

    protected void NewReply(object sender, DAL.PrivateMessage message)
    {
        mviewPMs.SetActiveView(viewNew);
        Newmessage1.ReplyToMessage(message);
    }
    protected void NewQuote(object sender, DAL.PrivateMessage message)
    {
        mviewPMs.SetActiveView(viewNew);
        Newmessage1.QuoteMessage(message);
    }

    protected void btnDeleteSelected_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gridMsgs.Rows)
        {
            HiddenField lblID = row.FindControl("lblID") as HiddenField;
            CheckBox chkDelete = row.FindControl("chkDelete") as CheckBox;
            if (lblID != null && chkDelete != null)
            {
                if (chkDelete.Checked)
                {
                    long id;
                    if (Int64.TryParse(lblID.Value, out id))
                    {
                        DAL.PrivateMessage pm = DAL.PrivateMessages.Item(id);
                        if (MsgMode == Mailbox.Inbox)
                        {
                            pm.ToDeleted = true;
                        }
                        else
                        {
                            pm.FromDeleted = true;
                        }

                        DAL.PrivateMessages.Save(pm);
                    }
                }
            }
        }
        Refresh();
    }
    protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
        List<DAL.PrivateMessage> pms;
        if (MsgMode == Mailbox.Inbox)
        {
            pms = DAL.PrivateMessages.GetInboxByUser(CurrentUser);
        }
        else if (MsgMode == Mailbox.Sent)
        {
            pms = DAL.PrivateMessages.GetSentboxByUser(CurrentUser);
        }
        else
        {
            pms = DAL.PrivateMessages.GetOutboxByUser(CurrentUser);
        }

        if (pms != null)
        {
            foreach (DAL.PrivateMessage pm in pms)
            {
                if (MsgMode == Mailbox.Inbox)
                {
                    pm.ToDeleted = true;
                }
                else
                {
                    pm.FromDeleted = true;
                }
                DAL.PrivateMessages.Save(pm);
            }
        }

        Refresh();
    }
}
